package com.sch.www.ele.controller;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.sch.www.codeGenerator.base.CustomPage;
import com.sch.www.common.base.BaseController;
import com.sch.www.common.base.ResultBean;
import com.sch.www.common.validator.NotBlankFluentValidator;
import com.sch.www.ele.entity.CareUnit;
import com.sch.www.ele.services.CareUnitServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("v1/careUnit")
@RestController
public class CareUnitController extends BaseController {


    @Autowired
    CareUnitServices careUnitServices;


    @RequestMapping(value = "list")
    private ResultBean<CustomPage<CareUnit>> list(
            String name,
            String creditCode,
            int pageNum,
            int pageSize) {

        ResultBean<CustomPage<CareUnit>> resultBean = ResultBean.newInstance();
        CustomPage<CareUnit> page = careUnitServices.list(name, creditCode, pageNum, pageSize);
        resultBean.setSuccess().setData(page);
        return resultBean;
    }



    @PostMapping("add")
    public ResultBean<CareUnit> addUnit( @RequestBody  CareUnit careUnit) {
        ResultBean<CareUnit> resultBean = ResultBean.newInstance();
        ComplexResult complexResult = FluentValidator.checkAll().failOver()
                .on(careUnit.getName(), new NotBlankFluentValidator("name"))
                .on(careUnit.getCreditCode(), new NotBlankFluentValidator("creditCode"))
                .on(careUnit.getLiaison(), new NotBlankFluentValidator("liaison"))
                .on(careUnit.getTelephone(), new NotBlankFluentValidator("telephone"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!complexResult.isSuccess()) {
            return resultBean.setError().appendMsg(complexResult);
        }
        careUnitServices.insertSelective(careUnit.getLiaison(), careUnit.getTelephone(), careUnit);
        return resultBean;
    }

    @PostMapping("modify")
    public ResultBean<CareUnit> modifyUnit(@RequestBody CareUnit careUnit) {
        ResultBean<CareUnit> resultBean = ResultBean.newInstance();
        ComplexResult complexResult = FluentValidator.checkAll().failOver()
                .on(careUnit.getName(), new NotBlankFluentValidator("name"))
                .on(careUnit.getCreditCode(), new NotBlankFluentValidator("creditCode"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!complexResult.isSuccess()) {
            return resultBean.setError().appendMsg(complexResult);
        }
        careUnitServices.updateByIdSelective(careUnit);
        return resultBean;
    }


}
